//
//  SEGTarget_Advertisement.m
//  SEGUhomeStandard
//
//  Created by 刘山 on 2018/3/22.
//  Copyright © 2018年 刘山. All rights reserved.
//

#import "SEGTarget_Advertisement.h"
#import "SEGAdvertisementViewController.h"
#import "SEGAdvertisementNewViewModel.h"

@interface SEGTarget_Advertisement()
@property(nonatomic, strong) SEGAdvertisementNewViewModel *viewModel;
@end

@implementation SEGTarget_Advertisement
-(void)segAction_showLaunchAndPopWindowAdvertisement:(NSDictionary*)params{

    SEGAdvlistType position = [[params allKeys] containsObject:@"position"]?(SEGAdvlistType)[[params valueForKey:@"position"] integerValue]:SEGAdvlistType_START;

    [[[[self.viewModel.requestCommand executionSignals] switchToLatest] deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(NSDictionary*  _Nullable x) {
        
        UIViewController *vc = (UIViewController*)[params valueForKey:@"vc"];
        
        SEGAdvertisementViewController *adVC = [SEGAdvertisementViewController new];
        adVC.adData = (SEGAdvertismentModel*)[x valueForKey:@"value"];
        adVC.positionType = [[x valueForKey:@"type"] integerValue];
        
        if ([[x valueForKey:@"type"] integerValue] == SEGAdvlistType_SYJ) {
            adVC.view.backgroundColor=[UIColor colorWithWhite:0 alpha:0.4];
            adVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
            [vc presentViewController:adVC animated:YES completion:nil];
            return;
        }
        
        adVC.modalPresentationStyle = UIModalPresentationFullScreen;
        [vc presentViewController:adVC animated:YES completion:nil];
    }];
    
    [self.viewModel.requestCommand execute:@(position)];
}

#pragma mark - getter
-(SEGAdvertisementNewViewModel *)viewModel{
    if (!_viewModel) {
        _viewModel = [[SEGAdvertisementNewViewModel alloc]init];
    }
    return _viewModel;
}
@end
